/**
 * @brief 云朵
 */
class SceneCloudy {
  constructor(viewer) {
    this.viewer = viewer;
    this.clouds = null;
  }
  //随机生成指定数量的经纬度
  generateCoordinate(numPoints) {
    const latMin = 31.38,
      latMax = 36.37;
    const lonMin = 110.21,
      lonMax = 116.65;
    let coordinates = [];
    for (let i = 0; i <= numPoints; i++) {
      let lat = Math.random() * (latMax - latMin) + latMin;
      let lon = Math.random() * (lonMax - lonMin) + lonMin;
      coordinates.push({ lon, lat, height: 10000 });
    }
    return coordinates;
  }
  /**
   * @brief 开启云朵效果
   * @param {Object} cloudParameters 云朵属性参数
   * @param {Number} num 云朵数量 例：1000
   */
  startCloudy(cloudParameters, num) {
    try {
      this.viewer.scene.primitives.remove(this.clouds);
      if (!this.cloudParams) {
        this.cloudParams = cloudParameters;
      }
      this.clouds = this.viewer.scene.primitives.add(
        new Cesium.CloudCollection({
          noiseDetail: 16.0,
          noiseOffset: Cesium.Cartesian3.ZERO
        })
      );
      let cloudsPointArr = this.generateCoordinate(num);
      cloudsPointArr.forEach((e) => {
        const { lon, lat, height } = e;
        let ePos = Cesium.Cartesian3.fromDegrees(lon, lat, height);
        this.clouds.add({
          position: ePos,
          scale: new Cesium.Cartesian2(
            cloudParameters.scaleX,
            cloudParameters.scaleY
          ),
          maximumSize: new Cesium.Cartesian3(
            cloudParameters.maximumSizeX,
            cloudParameters.maximumSizeY,
            cloudParameters.maximumSizeZ
          ),
          color: Cesium.Color[cloudParameters.color.toUpperCase()],
          slice: cloudParameters.renderSlice ? cloudParameters.slice : -1.0,
          brightness: cloudParameters.brightness
        });
      });
    } catch (error) {
      console.error(error);
    }
  }
  /**
   * @brief 关闭云朵效果
   */
  closeCloud() {
    this.viewer.scene.primitives.remove(this.clouds);
    this.cloudParams = null;
    this.clouds = null;
  }
}
export default SceneCloudy;
